package cn.btks.backstagemanage.log.controller;

import cn.btks.backstagemanage.log.pojo.SystemLog;
import cn.btks.backstagemanage.log.service.SystemLogService;
import cn.btks.commonserver.result.MenuSourceEnum;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.result.SystemLogEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author lijun
 * @Date 2021/1/4 18:47
 */
@RestController
@RequestMapping("api/v1.0/systemLog")
@Api(tags = "系统日志管理模块")
@Validated
public class SystemLogController {

    @Autowired
    SystemLogService systemLogService;

    @cn.btks.commonserver.valid.SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @GetMapping("/findPages")
    @ApiOperation("分页查询系统日志信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "userName", value = "操作人员名称（模糊查询）", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "operationName", value = "操作类型（查询、保存、修改、删除、登录、退出、异常）", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPagesByConfigNameAndConfigModeAndConfigCycle(Integer pageNumber, Integer pageSize, String userName, String operationName) {
        return systemLogService.findPagesByUserNameAndOperationName(pageNumber, pageSize, userName, operationName);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存系统日志信息", notes = "保存系统日志信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "角色ID", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "roleName", value = "角色名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "operationName", value = "操作名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "logMessage", value = "日志信息", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "objMessage", value = "操作对象信息", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "creatDateTime", value = "创建时间", required = false, dataType = "date"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(@RequestBody SystemLog systemLog) {
        SystemLog systemLog1 = systemLogService.save(systemLog);
        return systemLog1 != null ? new ResultState(StateInfo.SAVE_OK.getCode(), StateInfo.SAVE_OK.getMsg(), systemLog1) :
                new ResultState(StateInfo.SAVE_ERROR.getCode(), StateInfo.SAVE_ERROR.getMsg());
    }
}
